package com.wanpinghui.supplier.widgets;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.object.ObjectDetailActivity;
import com.wanpinghui.wphlibrary.model.message.bean.MsgBean;
import com.wanpinghui.wphlibrary.model.message.bean.MsgBidSuccessExt;
import com.yunshl.yunshllibrary.utils.TextUtil;

/**
 * Created by tg596 on 2018/1/13.
 */

public class MsgRightOrderNotificationView extends LinearLayout {


    private TextView tv_phone;
    private TextView tv_order_detail;
    private TextView tv_content;
    private TextView tv_type;
    private SpinView sv;
    private ImageView iv_error;
    private MsgBidSuccessExt msgBidSuccessExt;
    private TextView tv_msg;

    public MsgRightOrderNotificationView(Context context) {
        super(context);
        init(context);
    }

    public MsgRightOrderNotificationView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MsgRightOrderNotificationView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public MsgRightOrderNotificationView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void init(Context context) {
        View mView = LayoutInflater.from(context).inflate(R.layout.item_msg_right_order_notification, this);
        tv_order_detail = (TextView) mView.findViewById(R.id.tv_order_detail);
        tv_phone = (TextView) mView.findViewById(R.id.tv_phone);
        tv_msg = (TextView) mView.findViewById(R.id.tv_msg);
        tv_content = (TextView) mView.findViewById(R.id.tv_content);
        tv_type = (TextView) mView.findViewById(R.id.tv_type);
        sv = (SpinView) mView.findViewById(R.id.sv);
        iv_error = (ImageView) mView.findViewById(R.id.iv_error);

    }

    public void initData(MsgBean msgBean) {
        if (TextUtil.isNotEmpty(msgBean.getExt())) {
            try {
                msgBidSuccessExt = new Gson().fromJson(msgBean.getExt(), new TypeToken<MsgBidSuccessExt>() {
                }.getType());
            } catch (JsonSyntaxException e) {
                e.printStackTrace();
            }
        }
        if (msgBidSuccessExt != null) {
            tv_msg.setText(msgBean.getMsg());
            tv_type.setText(msgBidSuccessExt.getDemand_type());
            String message = msgBidSuccessExt.getDemand_info().replaceAll("\n\n","\n").replaceAll("\n\n","\n");
            final String phone = msgBidSuccessExt.getDemand_info().substring(message.length() - 11, message.length());
            if (TextUtil.isPhoneNumber(phone)) {
                SpannableString spannableString = new SpannableString(message);

                spannableString.setSpan(new ClickableSpan() {
                    @Override
                    public void onClick(View widget) {
                        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        getContext().startActivity(intent);
                    }

                    @Override
                    public void updateDrawState(TextPaint ds) {
                        ds.setUnderlineText(false);
                    }
                }, message.indexOf(phone), message.indexOf(phone) + 11, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getContext(), R.color.colorPrimaryBlue))
                        , message.indexOf(phone), message.indexOf(phone) + 11, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                tv_content.setText(spannableString);
                tv_content.setMovementMethod(LinkMovementMethod.getInstance());
            }else {
                tv_content.setText(message);
            }
            tv_order_detail.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getContext(), ObjectDetailActivity.class);
                    intent.putExtra("id", msgBidSuccessExt.getDemand_id());
                    intent.putExtra("notification", true);
                    getContext().startActivity(intent);
                }
            });
            switch (msgBean.getSend_statu()) {
                case 0:
                    setError(false);
                    setSending(true);
                    break;
                case 1:
                    setError(false);
                    setSending(false);
                    break;
                case 2:
                    setError(true);
                    setSending(false);
                    break;
            }
        }
    }

    public void setSending(boolean isSendding) {
        if (isSendding)
            sv.setVisibility(View.VISIBLE);
        else
            sv.setVisibility(View.GONE);

    }

    public void setError(boolean isError) {
        if (isError)
            iv_error.setVisibility(View.VISIBLE);
        else
            iv_error.setVisibility(View.GONE);
    }

}
